<?php

function dateFormat($date)
{
	return date_format(date_create($date), param('dateFormat'));
}

function array2print($array, $level=0){
	$level++;
	$content = "";
	$content .= "array(\n";
	if (!empty($array))
	{
		if (is_array($array))
		{
			$items = array();
			foreach ($array as $key => $value)
			{
				if (is_int($key))
				{
					$items[] = str_repeat("\t",$level)."'{$value}'";
				}
				else
				{
					if (is_array($value))
					{
						$items[] = str_repeat("\t",$level)."'{$key}' => ".array2print($value, $level);
					}
					else if (is_string($value))
					{
						$items[] = str_repeat("\t",$level)."'{$key}'=>'{$value}'";
					}
					else
					{
						$items[] = str_repeat("\t",$level)."'{$key}'=>{$value}";
					}
				}
			}
			$content .= implode(",\n", $items);
		}
		else
		{
			$content .= "'{$array}'";
		}
		$content .= "\n";
	}
	$content .= str_repeat("\t",--$level).")";
	
	return $content;
}

function param($name, $default = null) {
	if (isset(Yii::app()->params[$name]))
		return Yii::app()->params[$name];
	else
		return $default;
}

function tt($string, $module = null, $source = 'common') {
	if ($module === null) {
		if (Yii::app()->controller->module) {
			return Yii::app()->controller->module->t($string);
		}
		return Yii::t('common', $string);
	}
	return Yii::t($module.'Module.'.$source, $string);
}

function truncateText($text, $numOfWords = 10, $add = ''){
	if($numOfWords){
		$text = strip_tags($text, '<br/>');
		$text = str_replace(array("\r", "\n"), '', $text);

		$lenBefore = strlen($text);
		if($numOfWords){
			if(preg_match("/(\S+\s*){0,$numOfWords}/", $text, $match))
				$text = trim($match[0]);
			if(strlen($text) != $lenBefore){
				$text .= '.. '.$add;
			}
		}
	}

	return $text;
}

function is_file_writable($filename)
{
	@chmod($filename, 0777);
	return is_writeable($filename);
}

function installedModule($module)
{
	$sql = "SELECT id FROM {{modules}} WHERE name='{$module}'";
	$command = Yii::app()->db->createCommand($sql);
	return $command->queryRow();
}

function getSeoModules()
{
	$sql = "SELECT name FROM {{modules}} WHERE seo_module=1";
	$command = Yii::app()->db->createCommand($sql);
	return $command->queryAll();
}

function getLanguageModules()
{
	$sql = "SELECT name FROM {{modules}} WHERE lang_module=1";
	$command = Yii::app()->db->createCommand($sql);
	return $command->queryAll();
}

//function rrmdir($dir) {
//	if (is_dir($dir)) {
//		$objects = scandir($dir);
//		if($objects){
//			foreach ($objects as $object) {
//				if ($object != "." && $object != "..") {
//					if (filetype($dir . "/" . $object) == "dir")
//						rrmdir($dir . "/" . $object);
//					else
//						unlink($dir . "/" . $object);
//				}
//			}
//		}
//		reset($objects);
//		rmdir($dir);
//	}
//}
//
//function issetModule($module) {
//    if (is_array($module)) {
//        foreach ($module as $module_name) {
//            if (!isset(Yii::app()->modules[$module_name])) {
//                return false;
//            }
//        }
//        return true;
//    }
//    return isset(Yii::app()->modules[$module]);
//}
//
//function deb($mVal, $sName = '') {
//	$aCol = array('#FFF082','#BAFF81','#BAFFD7','#F0D9D7');
//	$color = $aCol[RAND(0,3)];
//	echo "<div style='background-color: $color;'><PRE><b>$sName = </b>";
//	if (is_array($mVal)) echo '<br>';
//	print_r($mVal);
//	echo "</PRE></div>";
//}
//
//function throw404(){
//	throw new CHttpException(404,'The requested page does not exist.');
//}